package common

import (
	"fmt"
	"io/ioutil"

	"gopkg.in/yaml.v3"
)

type Conf struct {
	Key     string  `yaml:"key"`
	AppPath string  `yaml:"apppath"`
	LogPath string  `yaml:"-"`
	CfgPath string  `yaml:"-"`
	Server  string  `yaml:"server"`
	Db      *ConfDb `yaml:"db"`
}

type ConfDb struct {
	Hostname string `yaml:"hostname"`
	Hostport int32  `yaml:"hostport"`
	Username string `yaml:"username"`
	Password string `yaml:"password"`
	Database string `yaml:"database"`
}

var Config *Conf

func init() {
	err := LoadConfig()
	if err != nil {
		fmt.Println("加载配置错误：", err.Error())
		return
	}
	Config.LogPath = Config.AppPath + "/conf/log"
	Config.CfgPath = Config.AppPath + "/conf/cfg.json"
}

func LoadConfig() (err error) {
	yamlFile, err := ioutil.ReadFile("conf.yaml")
	if err != nil {
		fmt.Println(err.Error())
	}
	err = yaml.Unmarshal(yamlFile, &Config)
	if err != nil {
		fmt.Println(err.Error())
	}
	return
}
